home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / 68hc11 / bin2int.c < prev    next >
C/C++ Source or Header  |  1994-10-20  |  3KB  |  94 lines

  1. /*
  2. Anyway, here is a simple little program which can be used on any machine
  3. with a C compiler and redirectable output (even 8 bitters!) and will work
  4. with any of the multitude of Data I/O programmers I am aware of, and most
  5. likely any programmer by any manufacturer, for that matter, since I think
  6. they all support this format (Intel Hex format).
  7.  
  8. I don't deal with byte order here, but that should only be a concern if
  9. you are doing word-wide programming on a gang-banger (in which case, just
  10. swap the devices in their sockets, if necessary :-) And don't forget Intel
  11. Hex format only has a 16 bit address field, so no files > 64k are allowed.
  12. */
  13. /*_ format83.c   Thu Sep 17 1987   Modified by: Erik Lindberg */
  14. /*
  15.       This program is released to the public domain. It can be
  16.       used for any purpose whatsoever. If you can get some poor
  17.       sucker to pay money for it, tell me your secret.
  18.  
  19.       Prints a file to stdout in Intel HEX 83 format. I wrote this
  20.       to be easy to understand, modify, and *portable*. If you want
  21.       elegant, you can get there from here.
  22. */
  23.  
  24. #include <stdio.h>
  25.  
  26. #define REC   0x10            /* Size of a record. */
  27.  
  28. char *line, buffer[128];
  29. FILE *infile;
  30.  
  31. extern char hex();
  32.  
  33. main(argc,argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.       int c=1, address=0;
  38.       int sum, i;
  39.       i=0;
  40.       if (!(infile = fopen(argv[++i],"rb"))) {
  41.               fprintf(stderr, "Error on open of file %s\n",argv[i]);
  42.               exit(1);
  43.       }
  44.  
  45.       while (c != EOF) {
  46.               sum = 0;
  47.               line = buffer;
  48.               for (i=0; i<REC && (c=getc(infile)) != EOF; i++) {
  49.                       *line++ = hex(c>>4);
  50.                       *line++ = hex(c);
  51.                       sum += c;               /* Checksum each character. */
  52.               }
  53.               if (i) {
  54.                       sum += address >> 8;    /* Checksum high address byte.*/
  55.                       sum += address & 0xff;  /* Checksum low address byte.*/
  56.                       sum += i;               /* Checksum record byte count.*/
  57.                       line = buffer;          /* Now output the line! */
  58.                       putchar(':');
  59.                       puthex(i,2);            /* Byte count. */
  60.                       puthex(address,4);      /* Do address and increment */
  61.                       address += i;           /*    by bytes in record.   */
  62.                       puthex(0,2);            /* Record type.            */
  63.                       for(i*=2;i;i--)         /* Then the actual data.   */
  64.                               putchar(*line++);
  65.                       puthex(0-sum,2);        /* Checksum is 1byte 2's comp.*/
  66.                       printf("\n");
  67.               }
  68.       }
  69.       printf(":00000001FF\n");                /* End record. */
  70. }
  71.  
  72. /* Return ASCII hex character for binary value. */
  73.  
  74. char hex(c)
  75. int c;
  76. {
  77.       if((c &= 0x000f)<10)
  78.               c += '0';
  79.       else
  80.               c += 'A'-10;
  81.       return((char) c);
  82. }
  83.  
  84. /* Put specified number of digits in ASCII hex. */
  85.  
  86. puthex(val,digits)
  87. int val,digits;
  88. {
  89.       if (--digits)
  90.               puthex(val>>4,digits);
  91.       putchar(hex(val & 0x0f));
  92. }
  93.  
  94.